﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.IO;
using System.Web.Security;
using System.Data.SqlClient;

using System.Configuration;

public partial class Admin_CandidateByRegNum : System.Web.UI.Page
{

    DataSet ds = null;
    string sqlQry = null;
    string strUserId, strCid;
    
    
   

    protected void Page_Load(object sender, EventArgs e)
     {
        if (!Page.IsPostBack)
        {
            //mp1.Hide();
            txtRegNum.Text = "";
            lblErrorMsg.Visible = false;

            //BtnDeleteReg.Attributes.Add("onclick", "javascript:validateRemark();");  
        }
    }
    void End_Block(Panel p)    //Clearing TextBoxes and DropdownLists
    {
        foreach (dynamic txtBox in p.Controls)
        {

            if (txtBox is TextBox)
                txtBox.Text = String.Empty;

        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        
        try
        {
            //BAL Method
            // objds= new UserMasterBAL.
            //mpe1.Hide();
        }
        catch (Exception)
        {
            //lblStatus1.Text = "Record is not deleted";
            //mpe1.Show();
        }
        finally
        {

        }
    }

    protected void Cancel_Click(object sender, EventArgs e)
    {
        //End_Block(panelAddNew);
    }

    //protected void lnkDel_Click(object sender, EventArgs e)
    //{
    //    if (Page.IsValid == true)
    //    {
            
            
    //        //GridViewRow row = gvByRegID.Rows[e.RowIndex];
    //        ////string strregistrationId = Convert.ToString(ViewState["RegistrationNo"]);
    //        //string strregistrationId = (int)gvByRegID.DataKeys[e.RowIndex].Value;
    //        //object objds1;
    //        //try
    //        //{
    //        //    objds1 = new UserMasterBAL().DeleteCandidatePhysical(strregistrationId);


    //        //}
    //        //catch (Exception exception)
    //        //{

    //        //    exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);

    //        //}



    //       // btnSearchRegId_Click(sender, e);

    //    }

    //    //try
    //    //{
    //    //    LinkButton lnk = sender as LinkButton;
    //    //    GridViewRow gr = (GridViewRow)lnk.NamingContainer;
    //    //   // string tempID = gv.DataKeys[gr.RowIndex].Value.ToString();
    //    //  //  ViewState["tempId"] = tempID;
    //    // //   sqlQry = "select empid from employee where empid=" + tempID;
    //    // //   if (cn.State != ConnectionState.Open)
    //    //    //    cn.Open();
    //    //  //  cmd = new SqlCommand(sqlQry, cn);
    //    //  //  dr = cmd.ExecuteReader();
    //    //    //if (dr.Read())
    //    // //   {
    //    //    //    lblId.Text = dr.GetValue(0).ToString();
    //    // //   }
    //    //  //  mpe3.Show();
    //    //    mp1.Show();
    //    //}
    //    //catch (Exception ex)
    //    //{
    //    //    return;
    //    //}
    //    //finally
    //    //{

    //    //}
    //}

    protected void btnDeleteReg_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            Int64 candID = Convert.ToInt64(ViewState["strCid"]);
            Guid userid = new Guid(Convert.ToString(ViewState["strUserId"]));
            
            string remarkbyadmin = txtRemark.Text.Trim();
            object objds1;
            try
            {
                objds1 = new UserMasterBAL().DeleteCandidateByRegNum(candID, userid, remarkbyadmin);


            }
            catch (Exception exception)
            {

                exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);

            }



            btnSearchRegId_Click(sender, e);

        }
    }

    protected void gvByRegID_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToUpper() == "POPUP")
        {

            
            mp1.Show();
        }
        //if(e.CommandName=="DeleteAll")
        //{

        //}

       
    }
    protected void btnSearchRegId_Click(object sender, EventArgs e)
    {
        string strRegNum = Convert.ToString(txtRegNum.Text.Trim());
        DataSet objDs;
        lblErrorMsg.Visible = false;
        gvByRegID.Visible = false;
        try
        {
            int isdeleted = 0;
            objDs = new UserMasterBAL().GetCandidateDetailsByRegNum(strRegNum);
            //GetCandidateDetails(objDs.Tables[1]);
            if (objDs.Tables[0].Rows.Count == 0)
            {
                  lblErrorMsg.Text = "Record Not Found..";
                lblErrorMsg.Visible = true;
            }
            else{
                isdeleted = Convert.ToInt32(objDs.Tables[0].Rows[0]["IsDeleted"]);
                gvByRegID.Visible = true;

                gvByRegID.DataSource = objDs;
                gvByRegID.DataBind();
                if (isdeleted == 1)
                {
                    //gvByRegID.Columns["Delete"].Visible = false;
                    //gvByRegID.Columns["Delete"].Visible = true;
                    //gvByRegID.Columns["Delete"].Visible = true;
                    gvByRegID.Columns[5].Visible = false;
                    // gvByRegID.Columns[6].Visible = true;
                    gvByRegID.Columns[7].Visible = true;
                    gvByRegID.Columns[8].Visible = true;


                }
                else
                {
                    gvByRegID.Columns[5].Visible = true;
                    gvByRegID.Columns[6].Visible = false;
                    gvByRegID.Columns[7].Visible = false;
                    gvByRegID.Columns[8].Visible = false;

                }
                ViewState["strUserId"] = Convert.ToString(objDs.Tables[0].Rows[0]["UserId"]);
                ViewState["strCid"] = Convert.ToString(objDs.Tables[0].Rows[0]["CandidateID"]);
                
            }
            

        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtRemark.Text = string.Empty;
        mp1.Hide();

    }

    public string GetViewtUrl(object objCandidateID)
    {
        string strPath = string.Empty;
        strPath = "~/home/CandidateDetailsReg.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID),true);

        return strPath;
    }


   
    


    protected void lnkDel_Click(object sender, EventArgs e)
    {
        mp1.Hide();
        //string strregistrationID = Convert.ToString(gvByRegID);
        //string strregistrationId = (gvByRegID.SelectedRow.Cells[0]).Text;
        //string country = (gvByRegID.SelectedRow.FindControl("RegistrationID")).ToString;
        //string strregistrationId = (gvByRegID.SelectedRow.Cells[1].Text);
        //string strregistrationId = txtRegNum.Text.Trim();
        //GridViewRow row = gvByRegID.SelectedRow;

        // In this example, the first column (index 0) contains
        //string strregistrationId = row.Cells[0].Text;
        foreach (GridViewRow gr in gvByRegID.Rows)
        {
            mp1.Hide();
            string strregistrationId;
               strregistrationId = Convert.ToString(gvByRegID.Rows[gr.RowIndex].Cells[1].Text);
            //string cell_2_Value= gvByRegID.Rows[gr.RowIndex].Cells[1].Text; 
               

            string strValue= string.Empty;
            try
            {
                strValue = (string)new UserMasterBAL().DeleteCandidatePhysical(strregistrationId);
               
                    if (strValue == "SUCCESS")
                    {
                        
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Candidate has been deleted succesfully.');", true);
                        gvByRegID.DataBind();
                        
                    }
                    else if (strValue == "NOTALLOW")
                    {
                        
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You have no Rights to delete this Candidate.');", true);
                        gvByRegID.DataBind();
                        //lblMessage.Text = "Brief already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //    
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while deleting Candidate.');", true);
                        gvByRegID.DataBind();
                        //lblMessage.Text = "Error while saving Brief."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                

            }
            catch (Exception exception)
            {

                exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);

            }
          
        }
    }
}
